import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import produce from 'immer'
import { ComponentPropsType } from '../../layouts/QuestionComponents'
export type ComponentInfoType = {
  fe_id: string
  type: string
  title: string
  props: ComponentPropsType
}
export type ComponentStateType = {
  componentList: Array<ComponentInfoType>
  selectedId?: string
}
const INIT_SATE: ComponentStateType = {
  componentList: [],
  selectedId: '',
}
export const componentSlice = createSlice({
  name: 'components',
  initialState: INIT_SATE,
  reducers: {
    resetComponents: (state: ComponentStateType, action: PayloadAction<ComponentStateType>) => {
      return action.payload
    },
    //修改selectedid
    // 修改 selectedId
    changeSelectedId: produce((draft: ComponentStateType, action: PayloadAction<string>) => {
      draft.selectedId = action.payload
    }),
  },
})
export const { resetComponents, changeSelectedId } = componentSlice.actions
export default componentSlice.reducer
